package com.laowang.project.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.laowang.common.exception.CustomException;
import com.laowang.common.utils.SecurityUtils;
import com.laowang.project.system.domain.MyCyhActivity;
import com.laowang.project.system.domain.MyCyhTicket;
import com.laowang.project.system.domain.vo.WxUserVo;
import com.laowang.project.system.service.MyCyhActivityService;
import com.laowang.project.system.service.MyCyhTicketService;
import javafx.scene.shape.CubicCurve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.laowang.project.system.mapper.MyCyhActivitySignMapper;
import com.laowang.project.system.domain.MyCyhActivitySign;
import com.laowang.project.system.service.MyCyhActivitySignService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动报名Service业务层处理
 *
 * @author laowang
 * @date 2023-05-22
 */
@Service
public class MyCyhActivitySignServiceImpl implements MyCyhActivitySignService {
    @Autowired
    private MyCyhActivitySignMapper myCyhActivitySignMapper;

    @Autowired
    private MyCyhActivityService myCyhActivityService;

    @Autowired
    private MyCyhTicketService myCyhTicketService;

    /**
     * 查询活动报名
     *
     * @param id 活动报名ID
     * @return 活动报名
     */
    @Override
    public MyCyhActivitySign selectMyCyhActivitySignById(Integer id) {
        return myCyhActivitySignMapper.selectMyCyhActivitySignById(id);
    }

    /**
     * 查询活动报名列表
     *
     * @param myCyhActivitySign 活动报名
     * @return 活动报名
     */
    @Override
    public List<MyCyhActivitySign> selectMyCyhActivitySignList(MyCyhActivitySign myCyhActivitySign) {
        return myCyhActivitySignMapper.selectMyCyhActivitySignList(myCyhActivitySign);
    }

    /**
     * 新增活动报名
     *
     * @param myCyhActivitySign 活动报名
     * @return 结果
     */
    @Override
    public int insertMyCyhActivitySign(MyCyhActivitySign myCyhActivitySign) {
        return myCyhActivitySignMapper.insertMyCyhActivitySign(myCyhActivitySign);
    }

    /**
     * 修改活动报名
     *
     * @param myCyhActivitySign 活动报名
     * @return 结果
     */
    @Override
    public int updateMyCyhActivitySign(MyCyhActivitySign myCyhActivitySign) {
        Integer auditStatus = myCyhActivitySign.getAuditStatus();
        if (auditStatus.equals(1)){
            myCyhActivitySign.setCheckTime(new Date());
        }
        return myCyhActivitySignMapper.updateMyCyhActivitySign(myCyhActivitySign);
    }

    /**
     * 批量删除活动报名
     *
     * @param ids 需要删除的活动报名ID
     * @return 结果
     */
    @Override
    public int deleteMyCyhActivitySignByIds(Integer[] ids) {
        return myCyhActivitySignMapper.deleteMyCyhActivitySignByIds(ids);
    }

    /**
     * 删除活动报名信息
     *
     * @param id 活动报名ID
     * @return 结果
     */
    @Override
    public int deleteMyCyhActivitySignById(Integer id) {
        return myCyhActivitySignMapper.deleteMyCyhActivitySignById(id);
    }


    @Transactional
    @Override
    public synchronized int insertMyCyhActivitySignWeb(MyCyhActivitySign myCyhActivitySign) {
        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();
        myCyhActivitySign.setUserId(mid);
        Integer activityId = myCyhActivitySign.getActivityId();

        MyCyhActivity myCyhActivity = myCyhActivityService.selectMyCyhActivityById(activityId);
        if (myCyhActivity.getAtimeEnd().before(new Date())){
            throw new CustomException("活动已过期",400);
        }


        //List<MyCyhActivitySign> myCyhActivitySigns = selectByIdAndUserId(activityId, mid);

        if (myCyhActivity.getIsFree().equals(1)){
            //Integer nums = myCyhActivity.getNums();
            Integer leave = myCyhActivity.getLeave();
            leave++;
            //myCyhActivity.setNums(nums);
            myCyhActivity.setLeave(leave);
            myCyhActivitySign.setIsPay(1);//免费就是已支付
            myCyhActivityService.updateMyCyhActivity(myCyhActivity);
        }

        myCyhActivitySign.setCtime(new Date());
        myCyhActivitySignMapper.insertMyCyhActivitySign(myCyhActivitySign);

        /*if (myCyhActivitySign1 !=null){
            *//*if (!myCyhActivity.getCate().equals("3")){
                throw new CustomException("已经报名过该活动了",400);
            }*//*
        }else {
            myCyhActivitySign.setCtime(new Date());
            myCyhActivitySignMapper.insertMyCyhActivitySign(myCyhActivitySign);
        }*/
        return 1;


    }

    @Override
    public MyCyhActivitySign  selectByIdAndUserId(Integer activityId, Integer userId) {
        return myCyhActivitySignMapper.selectByIdAndUserId(activityId,userId);
    }

    @Override
    public int insertMyCyhActivitySignWeb1(MyCyhActivitySign myCyhActivitySign) {
        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();
        myCyhActivitySign.setUserId(mid);
        Integer activityId = myCyhActivitySign.getActivityId();

        MyCyhActivity myCyhActivity = myCyhActivityService.selectMyCyhActivityById(activityId);
        if (myCyhActivity == null){
            throw new CustomException("报名的活动不存在",400);
        }
        if (myCyhActivity.getAtimeEnd().before(new Date())){
            throw new CustomException("活动已过期",400);
        }

        if (!myCyhActivity.getCate().equals("3")){
            Integer ticketId = myCyhActivitySign.getTicketId();
            if (!Objects.isNull(ticketId)){
                MyCyhTicket myCyhTicket = myCyhTicketService.selectMyCyhTicketById(ticketId);
                if (myCyhTicket == null){
                    throw new CustomException("票种不存在",400);
                }
                if (myCyhTicket.getIsEnable().equals(0)){
                    throw new CustomException("票种已下架",400);
                }

                Date startTime = myCyhTicket.getStartTime();
                Date endTime = myCyhTicket.getEndTime();
                if (new Date().after(startTime) && new Date().before(endTime)){

                }else {
                    throw new CustomException("票种还未开售或已到期",400);
                }
            }
            /*if (Objects.isNull(ticketId)){
                throw new CustomException("未选择票种");
            }*/

        }
        if (myCyhActivity.getNums() == null){
            throw new CustomException("请管理员先设置活动可报名人数");
        }
        if (myCyhActivity.getIsFree().equals(1)){
            Integer leave = myCyhActivity.getLeave();
            leave++;
            myCyhActivity.setLeave(leave);
            myCyhActivitySign.setIsPay(1);//免费就是已支付
            myCyhActivityService.updateMyCyhActivity(myCyhActivity);
        }

        myCyhActivitySign.setCtime(new Date());
        myCyhActivitySignMapper.insertMyCyhActivitySign(myCyhActivitySign);

        return 1;
    }

    @Override
    public List<MyCyhActivitySign> selectMyCyhActivitySignListByCreateBy(MyCyhActivitySign myCyhActivitySign) {
        return myCyhActivitySignMapper.selectMyCyhActivitySignListByCreateBy(myCyhActivitySign);
    }

    @Override
    public List<WxUserVo> selectUserById(Integer id,Integer userId) {
        return myCyhActivitySignMapper.selectUserById(id,userId);
    }
}
